using FireStarterModeller.BO;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.Logic.Commands
{
    public class RenameClassCommand : ICommand
    {
        private readonly DMClass _dmClass;
        private readonly string _newClassName;
        private string _originalName;

        public RenameClassCommand(DMClass dmClass, string newClassName)
        {
            _dmClass = dmClass;
            _newClassName = newClassName;
        }

        public void Apply()
        {
            _originalName = _dmClass.ClassNameBO;
            _dmClass.ClassNameBO = _newClassName;
            try
            {
                _dmClass.Save();
            }
            catch (HabaneroApplicationException)
            {
                _dmClass.Props["ClassNameBO"].RestorePropValue();
                throw;
            }
        }
        public void Revert()
        {
            _dmClass.ClassNameBO = _originalName;
            try
            {
                _dmClass.Save();
            }
            catch (HabaneroApplicationException)
            {
                _dmClass.Props["ClassNameBO"].RestorePropValue();
                throw;
            }
        }
    }
}